package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.page.LineDataInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.EchartDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户信息
 *
 * @author chengyu
 */
@RestController
@RequestMapping("/system/cost")
public class SysUserCostController extends BaseController
{
    @Autowired
    private ISysUserCostService userCostService;

    /**
     * 获取三餐数据，分页
     */
//    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserCost userCost)
    {
        startPage();
        List<SysUserCost> list = userCostService.selectUserCostList(userCost);
        return getDataTable(list);
    }



    /**
     * 新增数据
     */
//    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "新增数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUserCost userCost)
    {
        userCost.setCreateBy(getUsername());
        return toAjax(userCostService.insertUserCost(userCost));
    }

    /**
     * 删除
     */
//    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable List<String> userIds)
    {
//        if (ArrayUtils.contains(userIds, getUserId()))
//        {
//            return error("当前用户不能删除");
//        }
        return toAjax(userCostService.deleteUserCostByIds(userIds));
    }
    /**
     * 修改
     */
//    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUserCost userCost)
    {
//        userCost.setUpdateBy(getUsername());
        return toAjax(userCostService.updateUserCost(userCost));
    }

    /**
     * 获取根据id查询单条数据
     */
    @GetMapping("/{userId}")
    public AjaxResult getInfo(@PathVariable String userId)
    {
        SysUserCost userCost = userCostService.selectUserCostById(userId);
//        if (userCost == null) {
//            return AjaxResult.error("用户成本信息不存在");
//        }
        return AjaxResult.success(userCost);
    }

    /**
     * pie
     */

    @GetMapping("/pie")
    public EchartDataInfo pie(SysUserCostEchart userCostEchart)
    {
//        startPage();
        EchartData echartData = userCostService.selectUserCostPie(userCostEchart);
        return getEchartData(echartData);
    }
    @GetMapping("/line")
    public LineDataInfo line(SysUserCostEchart userCostEchart)
    {
        LineData lineData = userCostService.selectUserCostLine(userCostEchart);
        return getLineData(lineData);
    }

    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserCost userCost)
    {
        List<SysUserCost> list = userCostService.selectUserCostList(userCost);
        ExcelUtil<SysUserCost> util = new ExcelUtil<SysUserCost>(SysUserCost.class);
        util.exportExcel(response, list, "用户数据");
    }
}
